package com.lv.dhart.ui.fragment;

import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.lv.dhart.R;
import com.lv.dhart.custom.fragment.baseframework.BaseActivity;
import com.lv.dhart.custom.fragment.baseframework.BaseFragment;
import com.lv.dhart.custom.fragment.baseframework.interfaces.Layout;

import java.math.BigDecimal;
import java.math.RoundingMode;

@Layout(R.layout.fragment_as)
public class ASFragment extends BaseFragment<BaseActivity> {

    private Button computer2Btn;
    private Button computerBtn;
    private EditText highPriceEt;
    private EditText midPriceEt;
    private EditText lowPriceEt;
    private TextView huiTiaoTv;
    private TextView fanTanTv;
    private EditText originPriceEt;
    private EditText originCountEt;
    private EditText nowPriceEt;
    private EditText addCountEt;
    private EditText nowAmountEt;
    private TextView newPricePlusTv;
    private TextView newCountPlusTv;
    private TextView newPriceMinusTv;
    private TextView newCountMinusTv;

    @Override
    public void initViews() {
        computer2Btn = findViewById(R.id.btn_computer_range);
        computerBtn = findViewById(R.id.btn_computer_new_cost_count);
        highPriceEt = findViewById(R.id.et_highest_price);
        midPriceEt = findViewById(R.id.et_mid_price);
        lowPriceEt = findViewById(R.id.et_lowest_price);
        huiTiaoTv = findViewById(R.id.tv_price_up_range);
        fanTanTv = findViewById(R.id.tv_price_fall_range);
        originPriceEt = findViewById(R.id.et_origin_price);
        originCountEt = findViewById(R.id.et_origin_count);
        nowPriceEt = findViewById(R.id.et_current_price);
        addCountEt = findViewById(R.id.et_count_change);
        nowAmountEt = findViewById(R.id.et_amount_change);
        newPricePlusTv = findViewById(R.id.tv_new_price_by_add);
        newCountPlusTv = findViewById(R.id.tv_new_count_by_add);
        newPriceMinusTv = findViewById(R.id.tv_new_price_by_minus);
        newCountMinusTv = findViewById(R.id.tv_new_count_by_minus);
    }


    @Override
    public void initDatas() {

        computer2Btn.setOnClickListener(v -> {
            if (TextUtils.isEmpty(highPriceEt.getText().toString())
                    || TextUtils.isEmpty(midPriceEt.getText().toString())
                    || TextUtils.isEmpty(lowPriceEt.getText().toString())
            ) {
                Toast.makeText(requireContext(), "最高或最低价数据不完整！", Toast.LENGTH_SHORT).show();
                return;
            }
            BigDecimal highPrice = new BigDecimal(highPriceEt.getText().toString());
            BigDecimal midPrice = new BigDecimal(midPriceEt.getText().toString());
            BigDecimal lowPrice = new BigDecimal(lowPriceEt.getText().toString());
            BigDecimal diff = highPrice.subtract(midPrice);
            BigDecimal diff2 = midPrice.subtract(lowPrice);
            BigDecimal hui_tiao = diff.divide(highPrice, 3, RoundingMode.HALF_UP);
            BigDecimal fan_tan = diff2.divide(lowPrice, 3, RoundingMode.HALF_UP);
            fanTanTv.setText(fan_tan.toPlainString());
            huiTiaoTv.setText(hui_tiao.toPlainString());

        });
        computerBtn.setOnClickListener(view -> {
            if (TextUtils.isEmpty(originPriceEt.getText().toString())
                    || TextUtils.isEmpty(originCountEt.getText().toString())
                    || TextUtils.isEmpty(nowPriceEt.getText().toString())
                    || TextUtils.isEmpty(addCountEt.getText().toString())) {
                Toast.makeText(requireContext(), "数量或价格数据不完整！", Toast.LENGTH_SHORT).show();
                return;
            }

            //原始股价
            BigDecimal originPrice = new BigDecimal(originPriceEt.getText().toString());
            //原始数量
            BigDecimal originCount = new BigDecimal(originCountEt.getText().toString());
//                System.out.println("all: " + originPrice.multiply(originCount));
            //当前股价
            BigDecimal nowPrice = new BigDecimal(nowPriceEt.getText().toString());
            //当前预加减的数量
            BigDecimal addCount = new BigDecimal(addCountEt.getText().toString());
            //加仓后新的价格
            BigDecimal newPricePlus = originPrice.multiply(originCount)
                    .add(nowPrice.multiply(addCount))
                    .divide(originCount.add(addCount), 2, RoundingMode.HALF_UP);
            // 增减金额
            nowAmountEt.setText(nowPrice.multiply(addCount).toPlainString());
            newPricePlusTv.setText(newPricePlus.toPlainString());
            newCountPlusTv.setText(addCount.add(originCount).toPlainString());
            try {
                BigDecimal newPriceMinus = originPrice.multiply(originCount)
                        .subtract(nowPrice.multiply(addCount))
                        .divide(originCount.subtract(addCount), 2, RoundingMode.HALF_UP);
                newPriceMinusTv.setText(newPriceMinus.toPlainString());
                newCountMinusTv.setText(originCount.subtract(addCount).toPlainString());
            } catch (ArithmeticException e) {
                Toast.makeText(requireContext(), "估计你要清仓了！", Toast.LENGTH_SHORT).show();
            }

        });
    }


    @Override
    public void setEvents() {

    }


    @Override
    /**
     * 进入 Fragment 时调用此方法，isSwitchFragment 标记说明了是否为从别的 Fragment 切换至此 Fragment 的，
     * 若为 false，则有可能是从后台切换至前台触发
     */
    public void onShow(boolean isSwitchFragment) {
        log("IntroductionFragment: onShow");
        super.onShow(isSwitchFragment);
    }

    @Override
    public void onHide() {
        log("IntroductionFragment: onHide");
        super.onHide();
    }


}